package com.group.eams.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.FixedAssetQueryDto;
import com.group.eams.pojo.entity.FixedAsset;
import com.group.eams.pojo.vo.*;
import com.group.eams.service.FixedAssetService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 固定资产表(FixedAsset)表控制层
 *
 * @author DHUser
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/fixedAsset")
public class FixedAssetController  {
    /**
     * 服务对象
     */
    @Autowired
    private FixedAssetService fixedAssetService;

    /**
     * 通过主键查询单条数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public Result<FixedAsset> findById(@RequestParam Long id) {
        FixedAsset fixedAsset = fixedAssetService.findById(id);
        return Result.buildSuccess(fixedAsset);
    }

    /**
     * 分页查询
     * @param fixedAssetQueryDto
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(FixedAssetQueryDto fixedAssetQueryDto) {
        Page<FixedAssetListVo> fixedAssetPage = fixedAssetService.findByQueryDto(fixedAssetQueryDto);
        return Result.buildSuccess(fixedAssetPage.getTotal(), fixedAssetPage.getRecords());
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        List<FixedAsset> list = fixedAssetService.findAll();
        return Result.buildSuccess(list);
    }

    /**
     * 基础信息
     * @return
     */
    @RequestMapping(value = "/findBasicById",method = RequestMethod.GET)
    public Result findBasicById(@RequestParam Long id) {
        FixedBasicAssetVo fixedBasicAssetVo = fixedAssetService.findBasicById(id);
        return Result.buildSuccess(fixedBasicAssetVo);
    }
    /**
     * 采购验收信息
     * @return
     */
    @RequestMapping(value = "/findPurchaseById",method = RequestMethod.GET)
    public Result findPurchaseByAssetId(@RequestParam Long id) {
        FixedAssetPurchaseVo fixedAssetPurchaseVo = fixedAssetService.findPurchaseByAssetId(id);
        return Result.buildSuccess(fixedAssetPurchaseVo);
    }

    /**
     * 通过id查询财务信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findAssetAccountById",method = RequestMethod.GET)
    public Result findAssetAccountById(@RequestParam Long id) {
        FixedAssetAccountVo fixedAssetAccountVo = fixedAssetService.findAssetAccountById(id);
        return Result.buildSuccess(fixedAssetAccountVo);
    }

    /**
     * 通过id查询维保信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/findMaintenById",method = RequestMethod.GET)
    public Result findAssetMaintenById(@RequestParam Long id) {
        FixedAssetMaintenVo fixedAssetMaintenVo = fixedAssetService.findAssetMaintenById(id);
        return Result.buildSuccess(fixedAssetMaintenVo);
    }

    /**
     * 新增数据
     * @param fixedAsset
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody FixedAsset fixedAsset) {
        fixedAsset.setNumber("ASSET" + new Date().getTime());
        boolean result = fixedAssetService.insert(fixedAsset);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量新增数据
     * @param fixedAssetList
     * @return
     */
    @RequestMapping(value = "/addBatch",method = RequestMethod.POST)
    public Result addBatch(@RequestBody List<FixedAsset> fixedAssetList) {
        fixedAssetList.forEach(fixedAsset -> fixedAsset.setNumber("ASSET" + new Date().getTime()));
        boolean result = fixedAssetService.saveBatch(fixedAssetList);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 修改数据
     * @param fixedAsset
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody FixedAsset fixedAsset) {
        boolean result = fixedAssetService.update(fixedAsset);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量修改数据
     * @param fixedAssetList
     * @return
     */
    @RequestMapping(value = "/updateBatch",method = RequestMethod.POST)
    public Result updateBatch(@RequestBody List<FixedAsset> fixedAssetList) {
        boolean result = fixedAssetService.updateBatchById(fixedAssetList);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id) {
        boolean result = fixedAssetService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量删除数据
     * @param batchDeleteDto
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Result deleteBatch(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = fixedAssetService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
    
}

